//
//  USShoppingCartModel.m
//  usale0001
//
//  Created by YU on 16/2/24.
//  Copyright © 2016年 YU. All rights reserved.
//

#import "USShoppingCartModel.h"

static USShoppingCartModel *kUSUserShoppingCart;
#define kSCFileName [NSString stringWithFormat:@"USShoppingCart%@", [USUserManager sharedUserManager].userid]


@implementation USShoppingCartModel

+ (USShoppingCartModel *)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        kUSUserShoppingCart =  [USFileManager getObjectByFileName:kSCFileName];
        if (!kUSUserShoppingCart) {
             kUSUserShoppingCart = [[self alloc] init];
        }
    });
    
    return kUSUserShoppingCart;
}
+ (void)saveShoppingCartModel{
    
    [USFileManager saveObject:kUSUserShoppingCart byFileName:kSCFileName];
}

+ (void)shoppingCartReturnToZero {
    if (kUSUserShoppingCart.shoppingCartList.count != 0){
        for (USShoppingCartList *provider in kUSUserShoppingCart.shoppingCartList){
            provider.providerSelected = NO;
            for (USShoppingCartProductList *product in provider.buyProductList){
//                product.productDiscussPrice = nil;
                product.productSelected = NO;
            }
        }
        [USShoppingCartModel saveShoppingCartModel];
    }
    
}

+ (void)setAllSelect:(BOOL)selected{
    
    if (kUSUserShoppingCart.shoppingCartList.count != 0){
        for (USShoppingCartList *provider in kUSUserShoppingCart.shoppingCartList){
            
            provider.providerSelected = selected;
            for (USShoppingCartProductList *product in provider.buyProductList){
                product.productSelected = selected;
            }
        }
    }
}

+ (void)setAllPropertysToNil
{
    //*************遍历成员属性并置空*************
    unsigned int ivarsCnt = 0;
    //　获取类成员变量列表，ivarsCnt为类成员数量
    Ivar *ivars = class_copyIvarList([USShoppingCartModel class], &ivarsCnt);
    
    //　遍历成员变量列表，其中每个变量都是Ivar类型的结构
    for (int i = 0; i < ivarsCnt; i++) {
        Ivar ivar = ivars[i];
        object_setIvar([USShoppingCartModel sharedInstance], ivar, nil);
    }
}

- (NSMutableArray *)shoppingCartList
{
    if (!_shoppingCartList) {
        USShoppingCartModel *model = [USFileManager getObjectByFileName:kSCFileName];
        _shoppingCartList = model ? model.shoppingCartList.mutableCopy : [NSMutableArray array];
        
    }
    return _shoppingCartList;
}


@end

@implementation USShoppingCartList


@end

@implementation USShoppingCartProductList



@end
